import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;


/*
 
	* Created by flag on 2017-3-14 20:02:31

	*调用以下方法返回的数据：同种数据记录之间以 "\u0001" 分割，每条记录的数据项以 "\u0002" 分割。 不同种类的数据以  "\u0003" 分割。
	*调用以下方法需提供的参数必须按照每个方法的参数说明的顺序，每个参数之间以"\u0003"分割，如："user\u0003passwd\u0003phoneNum"。

 */

public class doHttp {

    private String IP = "localhost:8080/bbApp/";

//###############################################################################################################################
    public static synchronized String Welcome(){ //获取首页推送的帮助和笔记两条信息，两条信息之间用 “\u0003” 分割
        String param = "android=android";
        String Welcome = "welcome.view";
        return sendGet(Welcome,param);
    }
    public static synchronized String Login(String param){ //登录参数：用户名，密码
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&passwd="+str[1];
	res = res+"&android=android";
        String Login = "login.view";
        return sendPost(Login,res);
    }
    public static synchronized String Register(String param){ //注册参数：用户名，密码，手机，邮箱，学校，校区
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&passwd="+str[1]+"&phoneNum="+str[2];
        res = res+"&email="+str[3]+"&school="+str[4]+"&campus="+str[5];
	res = res+"&android=android";
        String Register = "register.view";
        return sendPost(Register,res);
    }

//###############################################################################################################################

    public static synchronized String getSeeHelpMsg(String user){ //查看所有求助信息，参数：用户名
        String res = "";
        res = "user="+user;
	res = res+"&android=android";
        String SeeHelpMsg = "showHelpPage.view";
        return sendGet(SeeHelpMsg,res);
    }
    public static synchronized String getSeeAhelp(String param){ //查看某一条具体的求助的详细信息，参数：用户名，求助ID
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&helpID="+str[1];
	res = res+"&android=android";
        String SeeAhelp = "showAHelpMsg.view";
        return sendGet(SeeAhelp,res);
    }
    public static synchronized String setIHelp(String param){ //在某条求助下面，处理：我帮助TA业务，参数：用户名，求助ID
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&studyNoteID="+str[1];
	res = res+"&android=android";
        String IHelp = "iHelpHim.view";
        return sendPost(IHelp,res);
    }
    public static synchronized String setPublishHelp(String param){ //处理发布求助信息，参数：用户名，摘要，详细描述，报酬
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&title="+str[1];
        res = res+"&describes="+str[2]+"&remuneration="+str[3];
	res = res+"&android=android";
        String PublishHelp = "handlePublishHelp.view";
        return sendPost(PublishHelp,res);
    }

// ###########################################################################################################################

    public static synchronized String getSeeNoteMsg(String user){ //查看所有学习笔记，参数：用户名
        user = "user="+user+"&android=android";
        String SeeNoteMsg = "showStudyNotePage.view";
        return sendGet(SeeNoteMsg,user);
    }
    public static synchronized String getSeeANote(String param){ //产看具体某一心得详细，参数：用户名，笔记
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&studyNoteID="+str[1];
	res = res+"&android=android";
        String SeeANote = "showAStudyNoteMsg.view";
        return sendGet(SeeANote,res);
    }
    public static synchronized String getSearchTitle(String title){ //按心得标题搜索，参数：标题
        title = "title="+title+"&android=android";
        String SearchTitle = "searceAStudyNote.view";
        return sendGet(SearchTitle,title);
    }
    public static synchronized String getSearchSubject(String subject){ //按学科搜索，参数：学科
        subject = "subject="+subject+"&android=android";
        String SearchSubject = "searchSubjectStudyNote.view";
        return sendGet(SearchSubject,subject);
    }
    public static synchronized String setPublishNote(String param){ //处理发布笔记，参数：用户名，标题，学科，内容
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&title="+str[1]+"&subject="+str[2];
        res = res+"&content="+str[3];
	res = res+"&android=android";
        String PublishNote = "handlePublishStudyNote.view";
        return sendPost(PublishNote,res);
    }
    public static synchronized String setIPraise(String param){ // 处理 赞了某心得：参数：用户名，心得ID
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&studyNoteID="+str[1];
	res = res+"&android=android";
        String IPraise = "iCollectThis.view";
        return sendPost(IPraise,res);
    }
    public static synchronized String setIGrade(String param){ // 处理 给某心得评分：参数：用户名，心得ID，分数
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&studyNoteID="+str[1]+"&grade="+str[2];
	res = res+"&android=android";
        String IGrade = "iCollectThis.view";
        return sendPost(IGrade,res);
    }
    public static synchronized String setICollect(String param){ //处理 收藏 某心得，参数：用户名，心得ID
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&studyNoteID="+str[1];
	res = res+"&android=android";
        String ICollect  = "iCollectThis.view";
        return sendPost(ICollect,res);
    }
    public static synchronized String setIComment(String param){ //处理 评论 某心得，参数：用户名，心得ID，评论内容
        String res = "";
        String[] str = param.split("\u0003");
        res = "user="+str[0]+"&studyNoteID="+str[1]+"&content="+str[2];
	res = res+"&android=android";
        String IComment = "iCommentThis.view";
        return sendPost(IComment,res);
    }

// ############################################################################################################################
    public static synchronized String getIHelpOthers(String user){ //获取我帮助别人的情况：参数：用户名
        user = "user="+user+"&android=android";
        String IHelpOthers = "showIHelpOthersMsg.view";
        return sendGet(IHelpOthers,user);
    }
    public static synchronized String getMyCollect(String user){ //获取我的收藏，参数：用户名
        user = "user="+user+"&android=android";
        String MyCollect = "showMyCollectMsg.view";
        return sendGet(MyCollect,user);
    }
    public static synchronized String getMySeekHelp(String user){ //获取我发布的求助，参数：用户名
       user = "user="+user+"&android=android";
        String MySeekHelp = "showMyPublishHelp.view";
        return sendGet(MySeekHelp,user);
    }
    public static synchronized String getMyNote(String user){ // 获取我发布的心得，参数：用户名
        user = "user="+user+"&android=android";
        String MyNote = "showMyPublishStudyNote.view";
        return sendGet(MyNote,user);
    }
    public static synchronized String getAboutMe(String user){ // 获取与我相关，参数：用户名
        user = "user="+user+"&android=android";
        String AboutMe = "showPersonalPage.view";
        return sendGet(AboutMe,user);
    }


   public static synchronized String EncodeSpecialChar(String str){
	str = str.replaceAll("\\*","\u002a");
	str = str.replaceAll("\\?","\u003f");
	str = str.replaceAll("\\&","\u0026");
	str = str.replaceAll("\\=","\u003d");
	str = str.replaceAll("\\.","\u002e");
	str = str.replaceAll("\\%","\u0025");
	str = str.replaceAll("\\!","\u0021");
	return str;

   }

   public static synchronized String DecodeSpecialChar(String str){
	str = str.replaceAll("\u002a","*");
	str = str.replaceAll("\u003f","?");
	str = str.replaceAll("\u0026","&");
	str = str.replaceAll("\u003d","=");
	str = str.replaceAll("\u002e",".");
	str = str.replaceAll("\u0025","%");
	str = str.replaceAll("\u0021","!");
	return str;

   }
 

// ############################################################################################################################

    private static synchronized String sendGet(String url, String param) { 
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("GET请求出现异常！\n" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    private static synchronized String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("POST 请求出现异常!\n"+e);
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String url = "http://localhost:8080/在线数据库";
        String param = "user=test";
        System.out.println(sendPost(url,param));
	String str = "aaaa?bbbb.ccc&ddd*eee%fff=ggg^hhh!iiii";
	System.out.println("\n\n\n"+EncodeSpecialChar(str));
    }


}
